// packageF/debt/vipAdd/vipAdd.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    debtId: '',
    listData: [],
    keyWord: '',


    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '会员管理',
    })
    if (options.debtId) {
      this.data.debtId = options.debtId
    }
    this.setData({
      plugin_setting:wx.getStorageSync('yz_basic_info').plugin_setting.debt_shop || {}
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  init() {
    this.setData({
      listData: [],
      isLoadMore: true,
      page: 1,
      total_page: 0
    })
  },
  blackTap() {
    wx.navigateBack({
      delta: 1,
    })
  },
  tapWkd() {
    this.init();
    this.getData();
    console.log(this.data.keyWord)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getData() {
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.owner.vipList');
    app._getNetWork({
      url: urlStr,
      data: {
        debtor_id: this.data.debtId,
        member_kwd: this.data.keyWord
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          console.log(res)
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            listData: res.data.data,
            owner_common_credit: res.data.owner_common_credit
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  addToVip(e) {
    let urls;
    if (e.currentTarget.dataset.key) {
      let vid = e.currentTarget.dataset.item.has_one_vip_member.uid;
      urls = '/packageF/debt/vipAddindex/vipAddindex?vid=' + vid + '&coupon=true' + '&debtId=' + this.data.debtId + '&credit=' + this.data.owner_common_credit + '&cvid=' + e.currentTarget.dataset.item.id
    } else {
      urls = '/packageF/debt/vipAddindex/vipAddindex?debtId=' + this.data.debtId + '&credit=' + this.data.owner_common_credit
    }
    wx.navigateTo({
      url: urls,
    })
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      urlStr = app.getNetAddresss('plugin.debt-shop.frontend.owner.vipList');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          debtor_id: this.data.debtId,
          member_kwd: this.data.keyWord
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              listData: this.data.listData.concat(res.data.data),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})